const { Tasks } = require('../services/tasks');
const { logger } = require('../utils/logger');

class AutomationController {
  // 截图
  static async screenshot(req, res) {
    try {
      const { url, engine = 'puppeteer', ...options } = req.body;
      
      if (!url) {
        return res.status(400).json({
          success: false,
          error: '缺少必要参数: url'
        });
      }
      
      const tasks = new Tasks(engine);
      const result = await tasks.screenshot(url, options);
      
      res.json({
        success: true,
        data: result,
        engine: tasks.getEngine()
      });
      
    } catch (error) {
      logger.error('截图失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  // 获取页面内容
  static async getContent(req, res) {
    try {
      const { url, engine = 'puppeteer', ...options } = req.body;
      
      if (!url) {
        return res.status(400).json({
          success: false,
          error: '缺少必要参数: url'
        });
      }
      
      const tasks = new Tasks(engine);
      const result = await tasks.getContent(url, options);
      
      res.json({
        success: true,
        data: result,
        engine: tasks.getEngine()
      });
      
    } catch (error) {
      logger.error('获取内容失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  // 填写表单
  static async fillForm(req, res) {
    try {
      const { url, formData, engine = 'puppeteer', ...options } = req.body;
      
      if (!url || !formData) {
        return res.status(400).json({
          success: false,
          error: '缺少必要参数: url, formData'
        });
      }
      
      const tasks = new Tasks(engine);
      const result = await tasks.fillForm(url, formData, options);
      
      res.json({
        success: true,
        data: result,
        engine: tasks.getEngine()
      });
      
    } catch (error) {
      logger.error('填写表单失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  // 点击元素
  static async click(req, res) {
    try {
      const { url, selector, engine = 'puppeteer', ...options } = req.body;
      
      if (!url || !selector) {
        return res.status(400).json({
          success: false,
          error: '缺少必要参数: url, selector'
        });
      }
      
      const tasks = new Tasks(engine);
      const result = await tasks.click(url, selector, options);
      
      res.json({
        success: true,
        data: result,
        engine: tasks.getEngine()
      });
      
    } catch (error) {
      logger.error('点击元素失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  // 豆包图片生成
  static async generateDoubaoImages(req, res) {
    try {
      const { prompt, engine = 'puppeteer', ...options } = req.body;
      
      if (!prompt) {
        return res.status(400).json({
          success: false,
          error: '缺少必要参数: prompt'
        });
      }
      
      const tasks = new Tasks(engine);
      const result = await tasks.generateDoubaoImages(prompt, options);
      
      res.json({
        success: true,
        data: result,
        engine: tasks.getEngine()
      });
      
    } catch (error) {
      logger.error('豆包图片生成失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  // 获取状态
  static async getStatus(req, res) {
    try {
      const browser = require('../services/browser');
      const status = browser.getStatus();
      
      res.json({
        success: true,
        data: {
          status: 'running',
          timestamp: new Date().toISOString(),
          uptime: process.uptime(),
          browser: status
        }
      });
      
    } catch (error) {
      logger.error('获取状态失败:', error.message);
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }
}

module.exports = AutomationController;
